package com.bob.light.config;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;

import com.bob.light.BuildConfig;
import com.bob.light.Tools;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AppConfig {
    private final Handler mHandler = new Handler(Looper.getMainLooper());
    private Application mApplication;
    private ExecutorService mExecutorService;
    private Boolean mDebug = BuildConfig.DEBUG;
    private Boolean mFirstRegister;
    private Builder mBuilder;

    static AppConfig mInstance;

    private AppConfig(Builder builder) {
        mInstance = this;
        init(builder);
    }

    private void init(Builder builder) {
        mBuilder = builder;
        mApplication = builder.application;
        mExecutorService = builder.executorService;
        mDebug = builder.debug;
        mFirstRegister = builder.firstRegister;
    }

    public static Handler uiHandler() {
        checkInitialization();
        return mInstance.mHandler;
    }

    public static Application application() {
        checkInitialization();
        return mInstance.mApplication;
    }

    public static ExecutorService executorService() {
        checkInitialization();
        return mInstance.mExecutorService;
    }

    public static boolean isDebug() {
        checkInitialization();
        return mInstance.mDebug;
    }

    public static boolean isFirstRegister() {
        checkInitialization();
        return mInstance.mFirstRegister;
    }

    public static Builder builder() {
        checkInitialization();
        return mInstance.mBuilder;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static class Builder {

        ExecutorService DEFAULT_EXECUTOR_SERVICE = Executors.newCachedThreadPool();
        Application application;
        ExecutorService executorService;
        boolean debug;
        private Boolean firstRegister = false;

        public Builder application(Application application) {
            this.application = application;
            return this;
        }

        public Builder executorService(ExecutorService executorService) {
            this.executorService = executorService;
            return this;
        }

        public Builder debug(boolean debug) {
            this.debug = debug;
            return this;
        }

        public Builder isFirstRegister(boolean isFirstRegister) {
            this.firstRegister = isFirstRegister;
            return this;
        }

        public AppConfig build() {
            if (this.executorService == null) {
                executorService = DEFAULT_EXECUTOR_SERVICE;
            }
            if (mInstance != null) {
                mInstance.init(this);
                return mInstance;
            }
            return new AppConfig(this);
        }
    }

    private static void checkInitialization() {
        Tools.requireNotNull(mInstance, "AppConfig not Initialization");
    }

}
